import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_09_game/base/widget/base_get_builder.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/widgets/button/common_button.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_09_game/widgets/input/common_text_field.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'login_logic.dart';

class LoginPage extends StatelessWidget {
  LoginPage({super.key});

  final LoginLogic logic = Get.put(LoginLogic());

  @override
  Widget build(BuildContext context) {
    return BaseGetBuilder<LoginLogic>(
      backgroundView: const Positioned(
          child: AutoImage(
        Assets.loginBg,
        fit: BoxFit.fitWidth,
      )),
      backgroundColor: AppColor.loginBgColor,
      builder: (l) {
        return SingleChildScrollView(
          physics: const ClampingScrollPhysics(),
          child: SafeArea(
              child: Column(
            children: [
              SizedBox(
                height: 44,
                child: Align(
                  alignment: Alignment.centerRight,
                  child: GestureDetector(
                    onTap: () {
                      Get.back();
                    },
                    behavior: HitTestBehavior.opaque,
                    child: SizedBox(
                      width: 44,
                      height: 44,
                      child: Center(
                        child: AutoImage(
                          Assets.commonCancelBtn,
                          width: 32.w,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
              110.verticalSpaceFromWidth,
              // _buildMain()
              Container(
                  height: 458.h,
                  margin: EdgeInsets.symmetric(horizontal: 16.w),
                  padding:
                      EdgeInsets.symmetric(vertical: 24.w, horizontal: 20.w),
                  decoration: BoxDecoration(
                    color: AppColor.pageBgColor,
                    borderRadius: BorderRadius.circular(24.w),
                  ),
                  child: Column(children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          'WELCOME TO ',
                          style: TextStyle(
                            color: AppColor.textPrimary.withOpacity(0.94),
                            fontSize: 24.sp,
                            fontWeight: FontWeight.w900,
                          ),
                        ),
                        Text(
                          LocaleKeys.appName.tr,
                          style: TextStyle(
                            color: AppColor.primary,
                            fontSize: 24.sp,
                            fontWeight: FontWeight.w900,
                          ),
                        ),
                      ],
                    ),
                    24.verticalSpaceFromWidth,
                    CommonTextField(
                          textEditingController: logic.isRegisterTypeRx
                              ? logic.registerAccountC
                              : logic.loginAccountC,
                          keyboardType: TextInputType.visiblePassword,
                          inputFormatters: [
                            FilteringTextInputFormatter.allow(
                                RegExp(r'[a-zA-Z0-9_]')),
                            LengthLimitingTextInputFormatter(20),
                            // 限制最大长度20位
                          ],
                          hintText: LocaleKeys.pleaseEnterUsername.tr,
                          maxLength: 20,
                          onChanged: (value) => logic.validateInput(),
                          // 触发验证
                          leftWidget: AutoImage(
                            Assets.loginUser,
                            width: 24.w,
                          ),
                        ),
                    12.verticalSpaceFromWidth,
                     CommonTextField(
                          textEditingController: logic.isRegisterTypeRx
                              ? logic.registerPwdC
                              : logic.loginPwdC,
                          isPassword: true,
                          keyboardType: TextInputType.visiblePassword,
                          inputFormatters: [
                            // 允许输入特殊字符，不做字符过滤
                            LengthLimitingTextInputFormatter(16),
                          ],
                          hintText: logic.isRegisterTypeRx
                              ? LocaleKeys.pleaseEnterPasswordLength.tr
                              : LocaleKeys.pleaseEnterPassword.tr,
                          maxLength: 16,
                          onChanged: (value) => logic.validateInput(),
                          // 触发验证
                          leftWidget: AutoImage(
                            Assets.loginPsw,
                            width: 24.w,
                          ),
                        ),
                    8.verticalSpaceFromWidth,
                    Expanded(
                        child: logic.isRegisterTypeRx
                            ? _buildMainRegister()
                            : _buildMainLogin())
                  ]))
            ],
          )),
        );
      },
    );
  }

  Widget _buildMainLogin() {
    return Column(
      children: [
        Visibility(
          visible: logic.isRegisterTypeRx,
          child: CommonTextField(
            textEditingController: logic.registerPwdAgainC,
            isPassword: true,
            keyboardType: TextInputType.visiblePassword,
            inputFormatters: [
              // 允许输入特殊字符，不做字符过滤
              LengthLimitingTextInputFormatter(16), // 限制最大长度10位
            ],
            hintText: LocaleKeys.pleaseConfirmPasswordLength.tr,
            maxLength: 16,
            // onChanged: (value) => logic.validateInput(),
            // 触发验证
            leftWidget: AutoImage(
              Assets.loginPsw,
              width: 24.w,
            ),
          ).marginOnly(bottom: 8.w),
        ),

        Visibility(
          visible: logic.isRegisterTypeRx,
          child: CommonTextField(
            textEditingController: logic.registerCodeC,
            // isPassword: true,
            keyboardType: TextInputType.visiblePassword,
            inputFormatters: [
              // 允许输入特殊字符，不做字符过滤
              LengthLimitingTextInputFormatter(16), // 限制最大长度10位
            ],
            hintText: LocaleKeys.pleaseEnterInviteCode.tr,
            maxLength: 16,
            // onChanged: (value) => logic.validateInput(),
            // 触发验证
            leftWidget: AutoImage(
              Assets.loginRecommend,
              width: 24.w,
            ),
          ).marginOnly(bottom: 8.w),
        ),

        // 记住密码和忘记密码行
        Row(
          children: [
            Obx(() => GestureDetector(
                  onTap: () => logic
                      .toggleRememberPassword(!logic.rememberPassword.value),
                  child: AutoImage(
                    logic.rememberPassword.value
                        ? Assets.commonCheckboxSel
                        : Assets.commonCheckboxNor,
                    width: 24.w,
                  ),
                )),
            2.horizontalSpace,
            Text(
              LocaleKeys.rememberPassword.tr,
              style: TextStyle(
                  fontSize: 14.sp,
                  color: AppColor.textPrimary.withOpacity(0.7)),
            ),
            const Spacer(),
            GestureDetector(
              onTap: () => logic.forgotPassword(),
              child: Text(
                LocaleKeys.forgotPassword.tr,
                style: TextStyle(fontSize: 14.sp, color: AppColor.primary),
              ),
            ),
          ],
        ),
        // 32.verticalSpaceFromWidth,
        const Spacer(),
        // 登录按钮
        Obx(() => CommonButton(
              title: LocaleKeys.login.tr,
              enable: logic.isLoginEnabled.value,
              onClick: () => logic.login(),
            )),
        24.verticalSpaceFromWidth,
        // 注册账户链接
        Center(
          child: GestureDetector(
            onTap: () => logic.registerAccount(true),
            child: Text(
              LocaleKeys.registerAccount.tr,
              style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w600,
                  color: AppColor.primary),
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildMainRegister() {
    return Column(
      children: [
        CommonTextField(
          textEditingController: logic.registerPwdAgainC,
          isPassword: true,
          keyboardType: TextInputType.visiblePassword,
          inputFormatters: [
            // 允许输入特殊字符，不做字符过滤
            LengthLimitingTextInputFormatter(16), // 限制最大长度10位
          ],
          hintText: LocaleKeys.pleaseConfirmPasswordLength.tr,
          maxLength: 16,
          // onChanged: (value) => logic.validateInput(),
          // 触发验证
          leftWidget: AutoImage(
            Assets.loginPsw,
            width: 24.w,
          ),
        ).marginOnly(bottom: 8.w),

        CommonTextField(
          textEditingController: logic.registerCodeC,
          // isPassword: true,
          keyboardType: TextInputType.visiblePassword,
          inputFormatters: [
            // 允许输入特殊字符，不做字符过滤
            LengthLimitingTextInputFormatter(16), // 限制最大长度10位
          ],
          hintText: LocaleKeys.pleaseEnterInviteCode.tr,
          maxLength: 16,
          // onChanged: (value) => logic.validateInput(),
          // 触发验证
          leftWidget: AutoImage(
            Assets.loginRecommend,
            width: 24.w,
          ),
        ).marginOnly(bottom: 8.w),

        // 32.verticalSpaceFromWidth,
        const Spacer(),
        // 注册按钮
        Obx(() => CommonButton(
              title: LocaleKeys.register.tr,
              enable: logic.isRegisterEnabled.value,
              onClick: () => logic.register(),
            )),
        24.verticalSpaceFromWidth,

        Row(mainAxisAlignment: MainAxisAlignment.center, children: [
          Text(
            LocaleKeys.alreadyHaveAccount.tr,
            style: TextStyle(
                // style: TextStyle(
                fontSize: 14.sp,
                fontWeight: FontWeight.w400,
                color: AppColor.textPrimary.withOpacity(0.94)),
          ),
          4.horizontalSpace,
          GestureDetector(
            onTap: () => logic.registerAccount(false),
            child: Text(
              LocaleKeys.login.tr,
              style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w600,
                  color: AppColor.primary),
            ),
          ),
        ])
      ],
    );
  }
}
